<?php
/**
 * Pages Plus widget class
 *
 * @since 2.8.0
 */
class Widget_Pages_Plus extends WP_Widget {

    function WP_Widget_Pages() {
        $widget_ops = array('classname' => 'widget_pages_plus', 
                            'description' => __( 'Selected WordPress Pages') );
        this->WP_Widget('pages_plus', __('Pages'), $widget_ops);
    }

    function widget( $args, $instance ) {
        extract( $args );

        $title = empty($instance['title']) ? "" : apply_filters('widget_title', $instance['title'] ); 
        $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
        $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
        $include = empty( $instance['include'] ) ? '' : $instance['include'];

        if ( $sortby == 'menu_order' )
            $sortby = 'menu_order, post_title';

        $out = wp_list_pages( 
            apply_filters('widget_pages_args', 
                          array('title_li' => '',
                                'echo' => 0,
                                'sort_column' => $sortby,
                                'exclude' => $exclude,
                                'include' => $include
                          ) 
            ) 
        );

        if ( !empty( $out ) ) {
            echo $before_widget;
            if ( !empty($title )) {
                echo $before_title . $title . $after_title;
            }
        ?>
        <ul>
            <?php echo $out; ?>
        </ul>
        <?php
            echo $after_widget;
        }
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) {
            $instance['sortby'] = $new_instance['sortby'];
        } else {
            $instance['sortby'] = 'menu_order';
        }

        $instance['exclude'] = strip_tags( $new_instance['exclude'] );
        $instance['include'] = strip_tags( $new_instance['include'] );

        return $instance;
    }

    function form( $instance ) 
    {
        //Defaults
        $instance = wp_parse_args(
            (array) $instance, 
            array( 'sortby' => 'post_title', 
                   'title' => '', 
                   'exclude' => '',
                   'include' => '') );

        $titleid = $this->get_field_id('title');
        $titlename = $this->get_field_name('title');
        $titleprompt = __( "Title:" );
        $titleval = esc_attr( $instance['title'] );

        $sortbyid = $this->get_field_id('sortby');
        $sortbyname = $this->get_field_name('sortby');
        $sortbyprompt = __( 'Sort by:' );

        $pagetitleprompt = __( 'Page title:' );
        $pageidprompt = __( "Page ID:" );

        $excludeid = $this->get_field_id('exclude');
        $excludename = $this->get_field_name('exclude');
        $excludeprompt = __( "Exclude:" );
        $excludeval = esc_attr( $instance['exclude'] );

        $pageidsprompt = __( "Page IDs, separated by commas." );

        $includeid = $this->get_field_id('include');
        $includename = $this->get_field_name('include');
        $includeprompt = __( "Include:" );
        $includeval = esc_attr( $instance['include'] );

    echo "
        <p><label for='{$titleid}'>{$titleprompt}</label>
        <input class='widefat' id='{$titleid}'
            name='{$titlename}'
            type='text' value='{$titleval}' />
        </p>
        ";
    echo "
        <p><label for='{$sortbyid}'>{$sortbyprompt}</label>
        <select name='{$sortbyname}'
            id='{$sortbyid}'
            class='widefat'>
        <option value='post_title'";
        selected( $instance['sortby'], 'post_title' ) ;
        echo ">";
        echo "{$pagetitleprompt}</option>
        <option value='menu_order'";
        selected( $instance['sortby'], 'menu_order' ); 
        echo ">{$pageorderprompt}</option>
        <option value='ID'";
        selected( $instance['sortby'], 'ID' ); 
        echo ">{$pageidprompt}</option>
        </select></p>
        ";

    echo "
        <p><label for='{$excludeid}'>{$excludeid}</label>
            <input type='text' value='{$excludeval}'
            name='{$excludename}'
            id='{$excludeid}' class='widefat' />
            <br />
            <small>{$pageidsprompt}</small>
        </p>
        ";

    echo "
        <p><label for='{$includeid}'>{$includeprompt}</label>
            <input type='text' value='{$includeval}'
            name='{$includename}'
            id='{$includeid}' class='widefat' />
            <br />
            <small>{$pageidsprompt}</small>
        </p>
        ";
<?php
}

function pages_plus_init() {
    register_widget('Widget_Pages_Plus');
}

add_action('init', 'pages_plus_init', 1);
?>
